import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import vuexP from 'vuex-persistedstate'

export default new Vuex.Store({
  plugins: [vuexP()],

  state: {
    // cart: [],
    todo: [],
    viewKey: "全部"
  },
  getters: {
    num1(state) {
      let arr
      arr = state.todo.filter(item => item.done == false)
      return arr.length
    },
    num2(state) {
      let arr
      arr = state.todo.filter(item => item.done == true)
      return arr.length
    },

    setView(state) {
      if (state.viewKey == '全部') {
        return state.todo
      }
      if (state.viewKey == "未完成") {
        return state.todo.filter(item => !item.done)
      }
      if (state.viewKey == "已完成") {
        return state.todo.filter(item => item.done)
      }
    }
  },
  mutations: {
    add(state, obj) {
      state.todo.push({
        id: new Date().getTime(),
        done: false,
        title: obj.title,
      })
    },
    //购物
    // addCart(state, obj) {
    //   console.log(obj);
    //   console.log(state.cart);
    //   let objArr = state.cart.find(item => item._id == obj._id)
    //  console.log(objArr);
    //   //   找到相同的商品   num++
    //   if (objArr) {
    //     objArr.num++
    //     return
    //   }
    //   obj.num = 1
    //   obj.status = false,///false就是不选中，true就是选中
    //     state.cart.push(obj)
    // },
    // ck(state,val){
    //   state.cart.forEach(item=>item.status=val)
    // },
    // changeSte(state,val){
    //   state.cart.num = val
    // }
    del(state, index) {
      state.todo.splice(index, 1)
    },
    clean(state) {
      state.todo = state.todo.filter(item => item.done == false)
    },
    statusChange(state, param) {
      const i = state.todo.findIndex(item => item.id == param.id)
      if (i != -1) {
        state.todo[i].done = param.status
      }
    },
    changeStatusAll(state) {
      // console.log(params)
      state.todo.forEach(item => {
        item.done = true
      })
    },
    // // 取消全选后的状态
    cleanStatusAll(state) {
      state.todo.forEach(item => {
        item.done = false
      })
    },
    changeView(state, key) {
      state.viewKey = key
    }
  },
  actions: {
  },
  modules: {
  }
})
